﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using HiSport.Application.HiSportService.Bas.BasVenueMemberManagement.Dtos;
using HiSport.Core.Entity;
using HiSport.Core.Entity.HiSportEntity.Bas;
using HiSport.Core.Util;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HiSport.Application.HiSportService.Bas.BasVenueMemberManagement.Service
{

    //[ApiDescriptionSettings("会员管理", Tag = "会员信息", SplitCamelCase = false)]
    [ApiDescriptionSettings("MemberManagement", Tag = "MemberInformation", SplitCamelCase = false)]
    public class BasVenueMemberGradeService : ITransient, IDynamicApiController, IBasVenueMemberService
    {
        /// <summary>
        /// 会员
        /// </summary>
        private readonly SqlSugarRepository<BasVenueMemberGrade> _venueMemberGrade;


        public BasVenueMemberGradeService(SqlSugarRepository<BasVenueMemberGrade> venueMemberGrade)
        {
            _venueMemberGrade = venueMemberGrade;

        }


        /// <summary>
        /// 查询 会员等级 分页
        /// </summary>
        /// <param name="indexDto"></param>
        /// <returns></returns>
        public async Task<SqlSugarPagedList<VenueMemberGradeDto>> Page(VenueMemberGradePageIndexDto inputDto)
        {
            return await _venueMemberGrade.AsQueryable()
                .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Name), u => u.Name.Contains(inputDto.Name.Trim()))
                .Select(u => new VenueMemberGradeDto()
                {
                    Id = u.Id.SelectAll(),
                })
                .ToPagedListAsync(inputDto.Page, inputDto.PageSize);
        }


        /// <summary>
        /// 新增会员等级
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>       
        [ApiDescriptionSettings(Name = "Create"), HttpPost]
        [DisplayName("新增会员等级")]
        public async Task<bool> Create(VenueMemberGradeCreateOrUpdateDto inputDto)
        {
            var res = true;
            var sysData = await _venueMemberGrade.GetFirstAsync(x => x.Name == inputDto.Name);
            if (sysData == null)
            {
                res = await _venueMemberGrade.InsertAsync(new BasVenueMemberGrade()
                {
                    Name = inputDto.Name,
                });
                if (!res)
                {
                    throw Oops.Oh("会员等级添加失败");
                }
            }
            else
            {
                throw Oops.Oh("会员等级已存在");
            }

            return res;
        }

        /// <summary>
        /// 修改会员等级名称
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>       
        [ApiDescriptionSettings(Name = "Update"), HttpPost]
        [DisplayName("修改会员等级名称")]
        public async Task<bool> Update(VenueMemberGradeCreateOrUpdateDto inputDto)
        {
            var res = true;
            var basData = await _venueMemberGrade.GetFirstAsync(x => x.Id == inputDto.Id);

            var basDataName = await _venueMemberGrade.GetFirstAsync(x => x.Name == inputDto.Name);
            if (basDataName!=null)
            {
                throw Oops.Oh("会员等级已存在");
            }
            if (basData != null)
            {
                basData.Name = inputDto.Name;
                res = await _venueMemberGrade.UpdateAsync(basData);
                if (!res)
                {
                    throw Oops.Oh("会员等级修改失败");
                }
            }
            else
            {
                throw Oops.Oh("会员等级不存在");
            }

            return res;
        }

    }
}
